package com.sk.control;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sk.domain.Shopcart;
import com.sk.domain.User;
import com.sk.service.ShopCartService;

@Controller
@RequestMapping("/shopcart")
public class ShopCartControl extends BaseControl {
	
	@Autowired
	private ShopCartService scs;
	@RequestMapping("addShopCart")
	public String addShopCart(@RequestParam("gid")int gid){
		User user = (User)session.getAttribute("user");
		scs.addShopCart(user,gid);
		
		return "success";
	}
	@RequestMapping("findAll")
	public String findAll(){
		User user = (User)session.getAttribute("user");
		List<Shopcart> sss = scs.findAll(user);
		
		double price = 0;
		for (Shopcart s : sss) {
			price = price + s.getGood().getPrice();
			
		}
		session.setAttribute("shopcarts", sss);
		session.setAttribute("price", price);
		return "redirect:/shopcart.jsp";
	}
	@RequestMapping("delShopCart")
	public String delShopCart(@RequestParam("id")int id){
		
		scs.delByID(id);
		
		return "springmvc/shopcart/findAll";
	}

}
